//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "InstrumentResults.h"
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::CInstrumentResults::CInstrumentResults (boost::shared_ptr<QuantLib::InstrumentResults>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInstrumentResults = NULL;
#endif
	_ppInstrumentResults = &childNative;
    
}
Cephei::QL::CInstrumentResults::CInstrumentResults (QuantLib::InstrumentResults& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phInstrumentResults = NULL;
#endif
	_ppInstrumentResults = new boost::shared_ptr<QuantLib::InstrumentResults> (&childNative);
    
    _InstrumentResultsOwner = owner;
    
}

Cephei::QL::CInstrumentResults::CInstrumentResults (CInstrumentResults^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInstrumentResults = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppInstrumentResults = new boost::shared_ptr<QuantLib::InstrumentResults> (copy->GetShared());
        
    }
}
Cephei::QL::CInstrumentResults::CInstrumentResults (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInstrumentResults = NULL;
#endif
	if (!t->IsSubclassOf(CInstrumentResults::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CInstrumentResults::CInstrumentResults (QuantLib::Handle<QuantLib::InstrumentResults>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phInstrumentResults = &childNative;
	_ppInstrumentResults = &static_cast<boost::shared_ptr<QuantLib::InstrumentResults>>(childNative.currentLink());
    
    _InstrumentResultsOwner = owner;
}
Cephei::QL::CInstrumentResults::CInstrumentResults (QuantLib::Handle<QuantLib::InstrumentResults> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phInstrumentResults = &childNative;
	_ppInstrumentResults = &static_cast<boost::shared_ptr<QuantLib::InstrumentResults>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CInstrumentResults::CInstrumentResults (QuantLib::InstrumentResults childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInstrumentResults = NULL;
#endif
	_ppInstrumentResults = new boost::shared_ptr<QuantLib::InstrumentResults> (new QuantLib::InstrumentResults (childNative));
    
}
#endif

Cephei::QL::CInstrumentResults::~CInstrumentResults ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppInstrumentResults != NULL)
    {
	    delete _ppInstrumentResults;
        _ppInstrumentResults = NULL;
    }
}
Cephei::QL::CInstrumentResults::!CInstrumentResults ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppInstrumentResults != NULL)
    {
	    delete _ppInstrumentResults;
    }
}
QuantLib::InstrumentResults& Cephei::QL::CInstrumentResults::GetReference ()
{
    if (_ppInstrumentResults == NULL) throw gcnew NativeNullException ();
	return **_ppInstrumentResults;
}
boost::shared_ptr<QuantLib::InstrumentResults>& Cephei::QL::CInstrumentResults::GetShared ()
{
    if (_ppInstrumentResults == NULL) throw gcnew NativeNullException ();
	return *_ppInstrumentResults;
}
QuantLib::InstrumentResults* Cephei::QL::CInstrumentResults::GetPointer ()
{
    if (_ppInstrumentResults == NULL) throw gcnew NativeNullException ();
	return &**_ppInstrumentResults;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::InstrumentResults>& Cephei::QL::CInstrumentResults::GetHandle ()
{
	if (_phInstrumentResults == NULL)
	{
		_phInstrumentResults = new Handle<QuantLib::InstrumentResults> (*_ppInstrumentResults);
	}
	return *_phInstrumentResults;
}
#endif
bool Cephei::QL::CInstrumentResults::HasNative () 
{
	return (_ppInstrumentResults != NULL);
}

Cephei::QL::IInstrumentResults^ Cephei::QL::CInstrumentResults::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppInstrumentResults)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

